﻿Imports System.Data.OleDb
Imports LecturerAppointmentSystem.UserDetails
Public Class Calendar
    Inherits System.Web.UI.Page

    Dim setLocation As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If User.IsInRole("Lecturer") Then
            CalendarMultiView.SetActiveView(LecturerView)
        ElseIf User.IsInRole("Student") Then
            CalendarMultiView.SetActiveView(StudentView)
        ElseIf User.IsInRole("Administrator") Then
            CalendarMultiView.SetActiveView(AdministratorView)
        End If
        Dim selectedDate As Date = Request.QueryString("Date")
        lbl_selectedDate.Text = selectedDate.DayOfWeek.ToString() & "," & MonthName(selectedDate.Month) & " " & selectedDate.Day & ", " & selectedDate.Year
        If Not IsPostBack Then
            LecturerGridView.DataBind()
            StudentGridView.DataBind()
        End If
    End Sub

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles AppointmentCalendar.SelectionChanged
        Response.Redirect("Calendar.aspx?Date=" & CType(AppointmentCalendar.SelectedDate, Date) & "&UserId=" & getUserId(User.Identity.Name))
    End Sub

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs) Handles AppointmentCalendar.DayRender
        Dim onmouseoverStyle As String = "this.style.backgroundColor='#D4EDFF'"
        Dim onmouseoutStyle As String = "this.style.backgroundColor='@BackColor'"
        Dim rowBackColor As String = String.Empty

        'e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
        'e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))

        If Not e.Day.IsWeekend AndAlso Not e.Day.IsToday Then
            e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
            e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
        End If

    End Sub

    Protected Sub Calendar1_Month(ByVal sender As Object, ByVal e As MonthChangedEventArgs) Handles AppointmentCalendar.VisibleMonthChanged
        Dim year As Integer = e.NewDate.Year
        Dim month As Integer = e.NewDate.Month
        Response.Redirect("Calendar.aspx?Date=1/" & month.ToString() & "/" & year.ToString() & "&UserId=" & getUserId(User.Identity.Name))
        AppointmentCalendar.SelectedDate = New DateTime(e.NewDate.Year, e.NewDate.Month, 1)

    End Sub

    Protected Function changeText(ByVal place As Object) As String
        Dim dbValue As String
        If IsDBNull(place) Then
            dbValue = ""
        Else
            dbValue = place.ToString().Trim()
        End If
        Return (If(dbValue = "", "Mark As Available", "Mark As Unavailable"))
    End Function

    Protected Function parseBooked(ByVal booked As Object) As String
        Dim dbValue As String = ""
        If IsDBNull(booked) Then
            dbValue = "N/A"
        ElseIf booked = "False" Then
            dbValue = "Not Booked"
        ElseIf booked = "True" Then
            dbValue = "Booked"
        End If
        Return dbValue
    End Function

    Protected Function getStatus(ByVal booked As Object) As String
        Dim dbValue As String = ""
        If IsDBNull(booked) Then
            dbValue = "No Appointment Scheduled"
        ElseIf booked = "False" Then
            dbValue = "No Appointment Scheduled"
        ElseIf booked = True Then
            dbValue = "Appointment Scheduled"
        End If
        Return dbValue
    End Function

    Protected Function getVisibility(ByVal booked As Object) As Boolean
        Dim visible As Boolean = False
        If IsDBNull(booked) Then
            visible = False
        ElseIf booked = "False" Then
            visible = False
        ElseIf booked = True Then
            visible = True
        End If
        Return visible
    End Function

    Function isAppointmentMarkedAsAvailable(ByVal clickedSlot As Integer) As Boolean
        Dim appointment As Boolean = False
        Dim sql As String = "SELECT * FROM Appointments WHERE AppointmentDate = @AppointmentDate AND Fkid_TimeSlot = @Fkid_TimeSlot AND Lecturer = @Lecturer"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@AppointmentDate", Request.QueryString("Date"))
        cmd.Parameters.AddWithValue("@Fkid_TimeSlot", clickedSlot)
        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.Read() Then
            appointment = True
        End If
        conn.Close()
        Return appointment
    End Function

    Function isAppointmentBooked(ByVal selectedDate As String, ByVal timeSlot As Integer)
        Dim booked As Boolean = False
        Dim sql As String = "SELECT * FROM Appointments WHERE AppointmentDate=@AppointmentDate AND Fkid_TimeSlot=@Fkid_TimeSlot AND Lecturer=@Lecturer"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@AppointmentDate", selectedDate)
        cmd.Parameters.AddWithValue("@Fkid_TimeSlot", timeSlot)
        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.Read() Then
            booked = reader("Booked").ToString()
        End If
        conn.Close()
        Return booked
    End Function

    Protected Sub ImgBtn_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        Dim btn_details As ImageButton = CType(sender, ImageButton)
        Dim row As GridViewRow = TryCast(btn_details.NamingContainer, GridViewRow)
        lbl_TimeSlot.Text = row.RowIndex.ToString()
        Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
        If (isAppointmentMarkedAsAvailable(timeSlot)) Then
            If isAppointmentBooked(Request.QueryString("Date"), timeSlot) Then
                CancelBookingPopup.Show()
            Else
                UnavailablePopup.Show()
            End If
        Else
            AvailablePopup.Show()
        End If
    End Sub

    Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
        If isAppointmentMarkedAsAvailable(timeSlot + 1) = False Then
            Dim sql As String = "INSERT INTO Appointments(AppointmentDate, Fkid_TimeSlot, Place, Agenda, Booked, Lecturer) VALUES(@AppointmentDate, @Fkid_TimeSlot, @Place, @Agenda, @Booked, @Lecturer)"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            conn.Open()
            cmd.Parameters.AddWithValue("@AppointmentDate", Request.QueryString("Date"))
            cmd.Parameters.AddWithValue("@Fkid_TimeSlot", timeSlot)
            cmd.Parameters.AddWithValue("@Place", tb_Place.Text)
            cmd.Parameters.AddWithValue("@Agenda", "")
            cmd.Parameters.AddWithValue("@Booked", 0)
            cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
            cmd.ExecuteNonQuery()
            conn.Close()
            LecturerGridView.DataBind()
        End If
    End Sub

    Protected Sub ButtonYes_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim AppointmentDate As Date = Request.QueryString("Date")
        Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
        If isAppointmentBooked(AppointmentDate, timeSlot) Then
            CancelBookingPopup.Show()
        Else
            Dim sql As String = "DELETE * FROM Appointments WHERE AppointmentDate = @AppointmentDate AND Fkid_TimeSlot = @Fkid_TimeSlot AND Lecturer = @Lecturer"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@AppointmentDate", AppointmentDate)
            cmd.Parameters.AddWithValue("@Fkid_Timeslot", timeSlot)
            cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            LecturerGridView.DataBind()
        End If

    End Sub

    Protected Sub ButtonOkCancel_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim AppointmentDate As Date = Request.QueryString("Date")
        Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
        Dim AppointmentId As Integer
        Dim sql As String = "SELECT ID FROM Appointments WHERE AppointmentDate = @AppointmentDate AND Fkid_TimeSlot = @Fkid_TimeSlot AND Lecturer = @Lecturer"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@AppointmentDate", AppointmentDate)
        cmd.Parameters.AddWithValue("@Fkid_TimeSlot", timeSlot)
        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
        conn.Open()
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.Read() Then
            AppointmentId = reader("ID")
        End If
        conn.Close()
        Response.Redirect("AppointmentDetails.aspx?ID=" & AppointmentId)
    End Sub
End Class